Grokking Database Fundamentals for Tech Interviews
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction to Database Systems
Course Overview
Overview of Databases
Core Components of Database
Database Architectures
Relational Vs. Non-relational Databases
Data Storage in DBMS
Data Storage Fundamentals
File Organization in DBMS
Data Compression and Encoding
Quiz
Indexing in DBMS
Introduction to Database Indexes
Single Level Indexing
Tree-Based Indexing Techniques
Hash-Based Indexing
Bloom Filters
Quiz
Transaction Management
Understanding the Transaction
Atomicity
Consistency
Isolation
Durability
Quiz
Understanding the Concurrency Control
Optimistic vs. Pessimistic Concurrency Control
Lock-Based Concurrency Control
Timestamp-Based Concurrency Control
Multiversion Concurrency Control (MVCC)
Quiz
Fundamentals of Distributed Databases
Partitioning in Databases
Partitioning Types
Sharding in Databases
Distributed Query Processing
Quiz
Replication in Databases
Replication Topologies
Understanding the Consistency Models in Databases
The CAP Theorem and Its Implications
Quiz
Understanding the Consensus
Consensus algorithms
Leader Election Strategies
Quiz
Understanding the Fault Tolerance
Logging Mechanisms in Databases
Checkpointing and Recovery Basics
Coordinated Recovery in Distributed Systems
High Availability Through Replication and Failover
Ensuring Data Resilience: Redundancy, Mirroring, and Recovery
Quiz
Introduction to NoSQL
Key-Value Stores
Document-Oriented Databases
Column-Family and Wide-Column Stores
Graph Databases
Quiz
Quora System Design & Database Design
TikTok System Design & Database Design
Airbnb System Design & Database Design
Snapchat System Design & Database Design
Atomicity

Atomicity is one of the core ACID properties that ensure the integrity of a database during transactions. It guarantees that each transaction is executed as a single, indivisible unit of work. This means a transaction will either complete all of its operations successfully or fail entirely, leaving the database unchanged.

What Does Atomicity Mean?

Image
  • All-or-Nothing Principle: Atomicity ensures that either all the operations in a transaction are applied or none of them are. There is no middle ground.
  • Commit and Rollback:
    • Commit: Successfully executing all operations in a transaction.
    • Rollback: Reverting all changes made during a transaction when a failure occurs.

Without atomicity, partial updates can leave the database in an inconsistent state, resulting in corrupted or incorrect data.

Example Scenario

Image

In the above example, there are two accounts, A and B:

  1. Initial State:

    • Account A: $30
    • Account B: $100
  2. Transaction Process:

    • Debit $10 from Account A (successful).
    • Credit $10 to Account B (failure due to an error).
  3. Final State:

    • Account A: $20 (incorrect partial execution).
    • Account B: $100 (unchanged, as credit failed).

Problem: Partial execution violates atomicity. The $10 is debited from A, but not credited to B. This leads to data inconsistency.

How Atomicity is Achieved in Databases

Databases implement atomicity through mechanisms like transaction logs, shadow copies, and recovery protocols. One of the simpler implementations of atomicity is the Shadow Copy technique.

Shadow Copy: An Atomicity Implementation

Shadow Copy is a technique where the database ensures atomicity by creating a separate copy of the database file for every transaction. Here’s how it works:

  1. Creating a Copy: When a transaction begins, the database creates a new copy of the database file (called the shadow copy).
  2. Performing Updates: Changes are made to the shadow copy, not the original file.
  3. Pointer Switch:
    • If the transaction is successful (commit), the database pointer is switched to the shadow copy, making it the new database.
    • If the transaction fails (rollback), the pointer remains on the original file, and the shadow copy is discarded.

Key Scenarios in Shadow Copy

  1. Successful Transaction:

    • All operations in the transaction complete.
    • The database pointer switches to the shadow copy, and the original copy is discarded.
  2. Failure in Pointer Update:

    • All operations succeed, but the pointer fails to update.
    • The transaction is aborted, and the original copy is retained.
  3. Failure During Execution:

    • If the transaction fails mid-way, the pointer remains on the original copy, discarding the incomplete shadow copy.

Limitations of Shadow Copy

  • Inefficiency: Creating a full copy of the database for each transaction consumes significant time and storage.
  • Single Transaction: Shadow copy assumes that only one transaction can run at a time, limiting concurrent processing.
  • Scalability: It does not scale well for high-throughput or multi-user environments.

Modern Alternatives for Atomicity

While shadow copy was an early implementation, modern databases achieve atomicity more efficiently using:

  1. Write-Ahead Logging (WAL): Logs changes before applying them to ensure rollback is possible.
  2. Two-Phase Commit (2PC): Ensures atomicity across distributed systems.
  3. Optimistic Concurrency Control: Tracks changes and applies them atomically only if no conflicts occur.

Why is Atomicity Important?

Atomicity ensures:

  1. Data Integrity: Prevents partial or corrupt updates during failures.
  2. Consistency: Maintains the database's expected state after transactions.
  3. Safe Retries: Developers can confidently retry a failed transaction without worrying about lingering changes.
Understanding the Transaction
Consistency
Mark as Completed

On this page

What Does Atomicity Mean?

Example Scenario

How Atomicity is Achieved in Databases

Shadow Copy: An Atomicity Implementation

Key Scenarios in Shadow Copy

Limitations of Shadow Copy

Modern Alternatives for Atomicity

Why is Atomicity Important?